package com.dinzeer.sjapadder.se;

import cn.mmf.slashblade_addon.SlashBladeAddon;
import cn.mmf.slashblade_addon.specialattacks.WaterDrive;
import com.dinzeer.legendreliclib.lib.util.check.CheckEntity;
import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import com.dinzeer.sjapadder.entity.SwordRainEntity;
import com.dinzeer.sjapadder.entity.SwordRainEntityLightning;
import com.dinzeer.sjapadder.register.SjaSpecialEffectRegsitry;
import mods.flammpfeil.slashblade.SlashBlade;
import mods.flammpfeil.slashblade.capability.slashblade.ISlashBladeState;
import mods.flammpfeil.slashblade.event.SlashBladeEvent;
import mods.flammpfeil.slashblade.registry.specialeffects.SpecialEffect;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;

import java.awt.*;

@Mod.EventBusSubscriber
public class Nightingales extends SpecialEffect {
    public Nightingales() {
        super(40,false,false);
    }
    @SubscribeEvent
    public static void onDoingSlash(SlashBladeEvent.DoSlashEvent event) {
        if(ModList.get().isLoaded(SlashBladeAddon.MODID)) {
            ISlashBladeState state = event.getSlashBladeState();
            if (state.hasSpecialEffect(SjaSpecialEffectRegsitry.NightingalesSE.getId())) {
                if (!(event.getUser() instanceof Player)) {

                    slash(event.getUser());
                    return;
                }

                Player player = (Player)event.getUser();
                int level = player.experienceLevel;
                if (SpecialEffect.isEffective(SjaSpecialEffectRegsitry.NightingalesSE.get(), level)) {

                    slash(player);

                }
            }
        }
    }








    public static void slash(LivingEntity livingEntity){
        int count = 3; // 总数20个
        double radius = 2.0; // 半径5*i1格
        double angleStep = 360.0 / count;
        double baseY = livingEntity.position().y +5;
        // 优化：将worldIn获取移到循环外部
        Level worldIn = livingEntity.level();
        // 新增：随机保底索引确保至少一个闪电实体
        int guaranteedIndex = livingEntity.getRandom().nextInt(count);
        int LightningIndex=90- SlashBladeUtil.getRefine(livingEntity.getMainHandItem());
        int extraBase=0;
        if (LightningIndex<0){
            extraBase=((LightningIndex*-1)-90)/2;
            LightningIndex=0;

        }
        for (int i = 0; i < count; i++) {
            SwordRainEntity ss;

            // 保底机制：当索引匹配时强制生成闪电实体
            if (i == guaranteedIndex) {
                ss = new SwordRainEntityLightning(SlashBlade.RegistryEvents.HeavyRainSwords, worldIn);
                ss.setColor(SlashBladeUtil.getColorCode(livingEntity.getMainHandItem()));
                ss.setDamage(SlashBladeUtil.getBaseAttackModifier(livingEntity.getMainHandItem())*0.02+(extraBase*0.2));
            }
            // 其他位置保持10%的闪电概率
            else if (livingEntity.getRandom().nextInt(100) > LightningIndex) {
                ss = new SwordRainEntityLightning(SlashBlade.RegistryEvents.HeavyRainSwords, worldIn);
                ss.setColor(SlashBladeUtil.getColorCode(livingEntity.getMainHandItem()));
                ss.setDamage(SlashBladeUtil.getBaseAttackModifier(livingEntity.getMainHandItem())*0.02+(extraBase*0.2));
            } else {
                ss = new SwordRainEntity(SlashBlade.RegistryEvents.HeavyRainSwords, worldIn);
                ss.setColor(Color.BLUE.getRGB());
            }
            worldIn.addFreshEntity(ss);
            ss.doFire();
            ss.setIsCritical(false);
            ss.setOwner(livingEntity);
            ss.setRoll(0);
            ss.setForward(true);
            // force riding
            ss.startRiding(livingEntity, true);








            double angle = Math.toRadians(angleStep * i);
            double xOffset = radius * Math.cos(angle);
            double zOffset = radius * Math.sin(angle);

            // 设置位置
            ss.setPos(
                    livingEntity.position().x + xOffset,
                    baseY, // 统一高度
                    livingEntity.position().z + zOffset
            );
            livingEntity.playSound(SoundEvents.CHORUS_FRUIT_TELEPORT, 0.2F, 1.45F);
        }
    }
}